package com.gitee.ywj1352.lerning.ag.common.context;

import com.gitee.ywj1352.lerning.ag.common.constants.UserConstants;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ywj on 2018/2/12.
 * 本类可以 存储上下文信息 如用户的登录信息 以及权限 等信息
 */
public class BaseContextHandler {

    public static ThreadLocal<Map<String,Object>> threadLocal=new ThreadLocal<Map<String, Object>>();


    public static void  set(String key,Object value){
        Map<String, Object> map = threadLocal.get();
        if(map==null){
            Map<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put(key,value);
            threadLocal.set(hashMap);
        }
        map.put(key,value);
    }


    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map==null){
            Map<String, Object> hashMap = new HashMap<String, Object>();
            threadLocal.set(hashMap);
        }
       return map.get(key);
    }

    public static void init(){
        Map<String, Object> map = threadLocal.get();
        if (map==null){
            Map<String, Object> hashMap = new HashMap<String, Object>();
            threadLocal.set(hashMap);
        }
    }


    public static void remove(){
        threadLocal.remove();
    }

    public static String getUserId(){
        String value=(String)get(UserConstants.USER_ID.toString());
        return value==null?null:value;
    }

    public static String getUserName(){
        String value=(String)get(UserConstants.USER_NAME.toString());
        return value==null?null:value;
    }

    public static List<String> getUserRoles(){
        List<String> value=(List<String>)get(UserConstants.USER_ROLES.toString());
        return value==null?null:value;
    }

    public static void setUserId(String userId){
        set(UserConstants.USER_ID.toString(),userId);
    }


    public static void setUserName(String userName){
        set(UserConstants.USER_NAME.toString(),userName);
    }


    public static void setUserRoles(List<String> roles){
        set(UserConstants.USER_ROLES.toString(),roles);
    }

}
